package com.zp.drug.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.zp.common.core.controller.BaseController;
import com.zp.common.core.entity.R;
import com.zp.common.core.validate.AddGroup;
import com.zp.common.core.validate.EditGroup;
import com.zp.common.excel.utils.ExcelUtil;
import com.zp.common.log.annotation.Log;
import com.zp.common.log.enums.BusinessType;
import com.zp.common.myBatis.core.page.PageQuery;
import com.zp.common.myBatis.core.page.TableDataInfo;
import com.zp.drug.entity.bo.DrugExportMasterBo;
import com.zp.drug.entity.vo.DrugExportMasterVo;
import com.zp.drug.service.IDrugExportMasterService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 药品出库主控制器
 * 前端访问路由地址为:/drug/drugExportMaster
 *
 * @author zhang peng
 * @ date 2024-05-23
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/drugExportMaster")
public class DrugExportMasterController extends BaseController {

    private final IDrugExportMasterService iDrugExportMasterService;

    /**
     * 查询药品出库主列表 (分页)
     */
    @SaCheckPermission("drug:drugExportMaster:list")
    @GetMapping("/selectPage")
    public TableDataInfo<DrugExportMasterVo> selectPage(DrugExportMasterBo bo, PageQuery pageQuery) {
        return iDrugExportMasterService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询药品出库主列表 (不分页)
     */
    @SaCheckPermission("drug:drugExportMaster:list")
    @GetMapping("/list")
    public R<List<DrugExportMasterVo>> list(DrugExportMasterBo bo) {
        List<DrugExportMasterVo> list = iDrugExportMasterService.queryList(bo);
        return R.ok(list);
    }

    /**
     * 导出药品出库主列表
     */
    @Log(title = "药品出库主", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(DrugExportMasterBo bo, HttpServletResponse response) {
        List<DrugExportMasterVo> list = iDrugExportMasterService.queryList(bo);
        ExcelUtil.exportExcel(list, "药品出库主", DrugExportMasterVo.class, response);
    }

    /**
     * 获取药品出库主详细信息
     *
     * @param exportId 主键
     */
    @GetMapping("/{exportId}")
    public R<DrugExportMasterVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable String exportId) {
        return R.ok(iDrugExportMasterService.queryById(exportId));
    }

    /**
     * 新增药品出库主
     */
    @SaCheckPermission("drug:drugExportMaster:add")
    @Log(title = "药品出库主", businessType = BusinessType.INSERT)
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody DrugExportMasterBo bo) {
        return toAjax(iDrugExportMasterService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改药品出库主
     */
    @Log(title = "药品出库主", businessType = BusinessType.UPDATE)
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody DrugExportMasterBo bo) {
        return toAjax(iDrugExportMasterService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除药品出库主
     *
     * @param exportIds 主键串
     */
    @Log(title = "药品出库主", businessType = BusinessType.DELETE)
    @DeleteMapping("/{exportIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable String[] exportIds) {
        return toAjax(iDrugExportMasterService.deleteWithValidByIds(Arrays.asList(exportIds), true) ? 1 : 0);
    }
}
